<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_xiaoeknow/model')->方法名();
*/
namespace jy_xiaoeknow;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function checkvip($isexi=false){
		$user=pdo_get('jy_xiaoeknow_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid']));		
		if(!$user['isvip']){
			if($isexi){
				exi('只有VIP会员才能操作！','error','/home/');
			}else{
				return false;
			}
		}elseif($user['viptime']<time()){
			if($isexi){
				exi('您的VIP已过期，无权操作！','error','/home/');
			}else{
				return false;
			}
		}
		return true;
	}
	//0表示系统的
	public function check_isaddqun($qun_id=0){
		$addlog=pdo_get('jy_xiaoeknow_quan_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>$_SESSION['onemerchid'],'qun_id'=>$qun_id));		
		if(!$addlog['isadd']){
			$pluginset=$this->plugin_setting('basic');
			if(!$pluginset['chat_id']){
				//exi('系统没有设置当前企业微信群ID（联系管理员-基本设置-企业微信设置）','error');
			}
			return false;
		}
		return true;
	}
	public function checklogin(){
		if(!$_SESSION['uid']){
			return false;
		}
		$user_id=pdo_getcolumn('jy_xiaoeknow_user',array('mid'=>$_SESSION['uid'],'merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),'id');
		$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','credit1','credit2','pcd','sex','mobile','groupid','avatar','nickname'));
		if(!$user_id){
			$insert=array(
				'mid'=>$_SESSION['uid'],
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'nickname'=>$member['nickname'],
				'avatar'=>$member['avatar'],
				'sex'=>$member['sex'],
				'city'=>$member['pcd'],
				'updatetime'=>TIMESTAMP,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_xiaoeknow_user',$insert);
		}
		return true;
	}
	public function checkpaohui($action=''){
		return true;
		$pluginset=$this->plugin_setting('basic');
		$url=custom_url($action);
		if(($pluginset['ispaohui']==1 || $_SESSION['uniacid']!=1) && $_SESSION['openid']){
			$num=count($pluginset['paohui_yuming']);
			if(!$num){
			  exi('请联系管理员设置炮灰域名！基本设置--炮灰机制','error');	
			}
			$num--;
			$index=rand(0,$num);
			$paohui_yuming=$pluginset['paohui_yuming'][$index]['yuming'];
			if($paohui_yuming != SITEROOT){
				$nowlink=$paohui_yuming.'/custom/'.$url;
				exi('','',$nowlink);
			}
		}
	}
	/*
		获取虚拟设置
	*/
	public function plugin_xuni($order){
		$xuni_types=$this->gift_type;
		$payset=uni_setting('pay');
		$creditdata=array();
		$video=$this->pdo_get('video',array('id'=>$order['video_id']),array('commissions'));
		foreach($xuni_types as $k=>$xv){
			$video['commissions']=json_decode($video['commissions'],true);
			$dirate=$video['commissions']['credit'.$k.'_dirate'];
			if(!$dirate){
				$dirate=$payset['credit'.$k.'_dirate']?:100;
			}
			//如果不允许
			if($video['commissions']['credit'.$k.'_diclose']){
				$dirate=0;
				continue;
			}
			$creditkey='credit'.$k;
			$rate=$payset['credit'.$k.'_rate']?:1;
			$creditdata[]=array(
				'rate'=>$rate,
				'dirate'=>$dirate,
				'type'=>$k,
				'field'=>$creditkey,
				'title'=>$xv,
			);
		}
		return $creditdata;
	}
	public function checknoread($item=array(),$isvip=0,$jie=array()){
		$item['noread']=false;
		if($jie['id']){
			$video=pdo_get('jy_xiaoeknow_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['video_id']));
			if($video['isjie'] && $item['jie_id']){
				if($video['type']>0){
					if($video['type']==2 && !$isvip){
						$item['noread']=true;
					}elseif($video['type']==1){
						$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$video['id']));			
						if(!$order['id'] && $video['price']>0){
							$item['noread']=true;
							$item['noread_type']=0;
							$item['noread_id']=$video['id'];
							$item['price']=$video['price'];
						}
						$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$video['id']));	
						if($order['id']){
							$item['order_id']=$order['id'];
						}
					}
				}elseif($jie['feetype']>0){
					if($jie['feetype']==2 && !$isvip){
						$item['noread']=true;
					}elseif($jie['feetype']==1){
						$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'jie_id'=>$jie['id']));			
						if(!$order['id'] && $jie['price']>0){
							$item['noread']=true;
							$item['noread_type']=1;
							$item['noread_id']=$jie['id'];
							$item['price']=$jie['price'];
						}
						$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'jie_id'=>$jie['id']));	
						if($order['id']){
							$item['order_id']=$order['id'];
						}
					}
				}else{
					if($item['feetype']>0){
						if($item['feetype']==2 && !$isvip){
							$item['noread']=true;
						}elseif($item['feetype']==1){
							$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'item_id'=>$item['id']));			
							if(!$order['id'] && $item['price']>0){
								$item['noread']=true;
								$item['noread_type']=2;
								$item['noread_id']=$item['id'];
							}
							$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'item_id'=>$item['id']));	
							if($order['id']){
								$item['order_id']=$order['id'];
							}
						}
					}
				}
			}else{
				if($video['type']>0){
					if($video['type']==2 && !$isvip){
						$item['noread']=true;
					}elseif($video['type']==1){
						$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$video['id']));			
						if(!$order['id'] && $video['price']>0){
							$item['noread']=true;
							$item['noread_type']=0;
							$item['price']=$video['price'];
							$item['noread_id']=$video['id'];
						}
						$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$video['id']));	
						if($order['id']){
							$item['order_id']=$order['id'];
						}
					}
				}else{
					if($item['feetype']>0){
						if($item['feetype']==2 && !$isvip){
							$item['noread']=true;
						}elseif($item['feetype']==1){
							$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'item_id'=>$item['id']));			
							if(!$order['id'] && $item['price']>0){
								$item['noread']=true;
								$item['noread_type']=2;
								$item['noread_id']=$item['id'];
							}
							$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'item_id'=>$item['id']));	
							if($order['id']){
								$item['order_id']=$order['id'];
							}
						}
					}
				}
			}
		}else{
			if($item['type']>0){
				if($item['type']==2 && !$isvip){
					$item['noread']=true;
				}elseif($item['type']==1){
					$order=pdo_get('jy_xiaoeknow_video_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));			
					if(!$order['id'] && $item['price']>0){
						$item['noread']=true;
						$item['noread_type']=0;
						$item['noread_id']=$item['id'];
					}
					$order=pdo_get('jy_xiaoeknow_video_order',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'video_id'=>$item['id']));	
					if($order['id']){
						$item['order_id']=$order['id'];
					}
				}
			}
		}
		return $item;
	}
}
?>